<!--
 * @Author: bzirs
 * @Date: 2022-12-25 10:24:15
 * @LastEditors: bzirs
 * @LastEditTime: 2022-12-29 21:44:26
 * @FilePath: /vue2-hr-two/src/views/departments/index.vue
 * @Description:
 *
 * Copyright (c) 2022 by bzirs, All Rights Reserved.
-->
<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- 顶部公司 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="openDialog('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 组织架构 -->
        <el-tree :props="defaultProps" :data="data" @node-click="handleNodeClick">
          <template v-slot="{data}">

            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="openDialog(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="handleEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="toRemoveDepts(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <!-- 弹窗 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <depts-dialog :id="deptsId" ref="refDeptDialog" :origin-list="originList" :is-edit.sync="isEdit" :show.sync="dialogVisible" @close="hDialogClose" @toUpdateList="toGetDepartments" />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartmentsList, removeDepartment } from '@/api/departments'
import { handleTreeArray } from '@/utils'
import deptsDialog from './components/deptsDialog.vue'
export default {
  components: { deptsDialog },
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },

      originList: [],

      // 弹窗
      dialogVisible: false,

      // 选中的id
      deptsId: '',
      isEdit: false

    }
  },
  created() {
    this.toGetDepartments()
  },
  methods: {
    // 打开dialog
    openDialog(id) {
      this.deptsId = id
      this.dialogVisible = true
    },
    // 关闭弹窗
    async  handleClose() {
      try {
        await this.$confirm('确认确定要关闭吗？', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })

        this.dialogVisible = false
      } catch (error) {
        console.log(error)
      }
    },
    handleNodeClick(data) {
      console.log(data)
    },
    // 获取部门列表
    async toGetDepartments() {
      const { data: { depts }} = await getDepartmentsList()
      depts.shift()

      this.originList = depts.map(({ id, pid, code, name }) => { return { id, name, code, pid } })
      this.data = handleTreeArray(depts)
    },
    // 编辑部门
    handleEdit(id) {
      this.isEdit = true
      this.dialogVisible = true
      this.deptsId = id
      // 4. 强制让子组件去发请求获取详情
      this.$nextTick(() => {
        // 调用子组件的loadDetail方法
        this.$refs.refDeptDialog.toGetDepartmentUser()
      })
    },
    // 只要 dialog showDialog从true变成false，这里就会执行
    hDialogClose() {
      // 调用子组件中的方法，来清空子组件中的表单
      this.$refs.deptDialog.resetForm()
      console.log('dialog, 关闭....')
    },
    // 删除部门
    async toRemoveDepts(id) {
      try {
        await this.$confirm('确认确定要删除吗？', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })

        const res = await removeDepartment(id)
        console.log(res)
        this.toGetDepartments()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
